/**
 * @projectName leetode
 * @package cn.leetcode
 * @className cn.leetcode.t98
 * @copyright Copyright 2020 Thunisoft, Inc All rights reserved.
 */
package cn.leetcode;

/**
 * t98
 * @description
 * @author fei
 * @date 2025/9/12 11:10
 * @version 1.0
 */
public class t98 {
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        return valid(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }

    private boolean valid(TreeNode root, long left, long right) {
        if (root == null) {
            return true;
        }
        long t = root.val;
        return left < t && t < right &&
                valid(root.left, left, t) &&
                valid(root.right, t, right);
    }
}
